package com.xyht.medicine_management_system.modules.classification.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.classification.entity.MmsMedicineClassification;
import com.xyht.medicine_management_system.modules.classification.entity.req.MmsMedicineClassificationAddReq;
import com.xyht.medicine_management_system.modules.classification.entity.req.MmsMedicineClassificationUpdateReq;
import com.xyht.medicine_management_system.modules.classification.entity.resp.MmsMedicineClassificationDetailResp;
import com.xyht.medicine_management_system.modules.classification.mapper.MmsMedicineClassificationMapper;
import com.xyht.medicine_management_system.modules.classification.service.MmsMedicineClassificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.medicine_management_system.modules.user.entity.MmsUser;
import com.xyht.medicine_management_system.modules.user.mapper.MmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.user.constant.QualificationTypeConstant.QUALIFICATION_TYPE_MEDICINE;
import static com.xyht.medicine_management_system.modules.user.constant.QualificationTypeConstant.QUALIFICATION_TYPE_NONE;

/**
 * <p>
 * 药品分类 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-11-29
 */
@Service
public class MmsMedicineClassificationServiceImpl extends ServiceImpl<MmsMedicineClassificationMapper, MmsMedicineClassification> implements MmsMedicineClassificationService {

    @Resource
    private MmsMedicineClassificationMapper classificationMapper;
    @Resource
    private MmsUserMapper userMapper;


    @Override
    public ResponseResult addClassification(MmsMedicineClassificationAddReq addReq) {
        if (isNullOrEmpty(addReq.getName(),addReq.getDescription())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //校验分类名是否重复
        List<MmsMedicineClassification> classificationList = classificationMapper.selectList(new LambdaQueryWrapper<>())
                .stream()
                .filter(classification -> classification.getName().equals(addReq.getName()))
                .collect(Collectors.toList());
        if (isNullOrEmpty(classificationList)) {
            return CommonResult.failed(CommonCodeEnum.MEDICINE_CLASSIFICATION_NAME_REPEAT);
        }

        MmsMedicineClassification addMedicineClassification = new MmsMedicineClassification();
        BeanUtils.copyProperties(addReq,addMedicineClassification);
        classificationMapper.insert(addMedicineClassification);

        return CommonResult.success();
    }

    @Override
    public ResponseResult updateClassification(MmsMedicineClassificationUpdateReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(),updateReq.getName(),updateReq.getDescription())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //校验分类名是否重复
        List<MmsMedicineClassification> classificationList = classificationMapper.selectList(new LambdaQueryWrapper<>())
                .stream()
                .filter(classification -> classification.getName().equals(updateReq.getName()))
                .collect(Collectors.toList());
        if (isNullOrEmpty(classificationList)) {
            return CommonResult.failed(CommonCodeEnum.MEDICINE_CLASSIFICATION_NAME_REPEAT);
        }

        MmsMedicineClassification updateMedicineClassification = classificationMapper.selectById(updateReq.getId());

        if (isNullOrEmpty(updateMedicineClassification)) {
            return CommonResult.failed(CommonCodeEnum.MEDICINE_CLASSIFICATION_NOT_EXIST);
        }
        BeanUtils.copyProperties(updateReq,updateMedicineClassification);
        classificationMapper.updateById(updateMedicineClassification);

        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedClassification(String id) {
        if (isNullOrEmpty(id)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsMedicineClassification medicineClassification = classificationMapper.selectById(id);

        if (isNullOrEmpty(medicineClassification)) {
            return CommonResult.failed(CommonCodeEnum.MEDICINE_CLASSIFICATION_NOT_EXIST);
        }

        classificationMapper.deleteById(id);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getClassificationDetail(String id, String userId) {

        MmsMedicineClassificationDetailResp mmsMedicineClassificationDetailResp = new MmsMedicineClassificationDetailResp();
        if (isNullOrEmpty(id)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsUser mmsUser = userMapper.selectById(userId);
        if (isNullOrEmpty(mmsUser)) {
            return CommonResult.failed(CommonCodeEnum.USER_NOT_EXIST);
        }
        //查用户资质
        Integer qualificationType = mmsUser.getQualificationType();

        MmsMedicineClassification medicineClassification = classificationMapper.selectById(id);

        if (isNullOrEmpty(medicineClassification)) {
            return CommonResult.failed(CommonCodeEnum.MEDICINE_CLASSIFICATION_NOT_EXIST);
        }
        //查分类资质
        Integer isQualification = medicineClassification.getIsQualification();

        //需要
        if (isQualification == QUALIFICATION_TYPE_MEDICINE && !isQualification.equals(qualificationType)) {
            return CommonResult.failed(CommonCodeEnum.MEDICINE_NOT_AUTH);
        }

        BeanUtils.copyProperties(medicineClassification, mmsMedicineClassificationDetailResp);

        return CommonResult.success(mmsMedicineClassificationDetailResp);
    }

    @Override
    public ResponseResult getClassificationList(Integer pageNum, Integer pageSize, String name, String userId, Integer type) {
        Page<MmsMedicineClassification> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<MmsMedicineClassification> classificationLambdaQueryWrapper = new LambdaQueryWrapper<MmsMedicineClassification>()
                .orderByDesc(MmsMedicineClassification::getCreateTime);
        if (isNullOrEmpty(userId)) {
            classificationLambdaQueryWrapper.eq(MmsMedicineClassification::getIsQualification, QUALIFICATION_TYPE_NONE);
        } else {
            MmsUser mmsUser = userMapper.selectById(userId);
            if (mmsUser != null) {
                //查用户资质
                Integer qualificationType = mmsUser.getQualificationType();
                if (qualificationType == QUALIFICATION_TYPE_NONE) {
                    classificationLambdaQueryWrapper.eq(MmsMedicineClassification::getIsQualification, qualificationType);
                }
            } else {
                classificationLambdaQueryWrapper.eq(MmsMedicineClassification::getIsQualification, QUALIFICATION_TYPE_NONE);
            }
        }
        if (!isNullOrEmpty(name)) {
            classificationLambdaQueryWrapper.like(MmsMedicineClassification::getName, name);
        }
        if (!isNullOrEmpty(type)) {
            classificationLambdaQueryWrapper.eq(MmsMedicineClassification::getIsQualification, type);
        }
        classificationMapper.selectPage(page, classificationLambdaQueryWrapper);
        List<MmsMedicineClassificationDetailResp> classificationList = page.getRecords()
                .stream()
                .map(filterClassification -> {
                    MmsMedicineClassificationDetailResp medicineClassificationResp = new MmsMedicineClassificationDetailResp();
                    BeanUtils.copyProperties(filterClassification, medicineClassificationResp);
                    return medicineClassificationResp;
                })
                .collect(Collectors.toList());
        int total = classificationList.size();
        return CommonResult.success(classificationList, total);
    }
}
